# -*- coding: utf-8 -*-
"""
Created on Fri May  6 16:06:28 2022

@author: fengmingshan
"""


import os
path = r'D:\2022年工作\2022年5月爱立信电联共享开通脚本制作程序推广'
os.chdir(path)
share_file = '共享基站清单 - 副本.xlsx'
df_cs_fallback = pd.read_excel(share_file,sheet_name = '小区清单')
df_cs_fallback = df_cs_fallback[['ENODEBID','cellName','回落策略','频段','频点']]


def add_gran_freq_group(file,band):
    with open(file,'a') as f:
        command1 = 'cr ENodeBFunction=1,GeraNetwork=1\n'
        command2 = 'cr ENodeBFunction=1,GeraNetwork=1,GeranFreqGroup=1800\n'.format(band)
        command3 = '1\n'
        command4 = 'cr ENodeBFunction=1,GeraNetwork=1,GeranFreqGroup=900\n'.format(band)
        command5 = '2\n'
        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines(command4)
        f.writelines(command5)
        f.writelines('\n')


def add_gran_freq(file,band,freq):
    with open(file,'a') as f:
        command1 = 'cr ENodeBFunction=1,GeraNetwork=1,GeranFrequency={band}_{freq}\n'.format(
            band = band,
            freq = freq,
        )
        command2 = '{}\n'.format(band)
        command3 = '0\n'
        f.writelines(command1)
        f.writelines(command2)
        f.writelines(command3)
        f.writelines('\n')


def add_utran_freq(file,band):
    with open(file,'a') as f:
        f.writelines('crn ENodeBFunction=1,UtraNetwork=1'+'\n')
        f.writelines('end'+'\n')
        f.writelines('\n')
        f.writelines('crn ENodeBFunction=1,UtraNetwork=1,GeranFreqGroup={band}'.format(band = band)+'\n')
        f.writelines('end'+'\n')
        f.writelines('\n')

def add_csfall_back2gran(file,freq,cellnames):
    with open(file,'a') as f:
        for cellname in cellnames:
            f.writelines('cr ENodeBFunction=1,EUtranCellFDD={cellname},GeranFreqGroupRelation={freq}'.format(cellname = cellname,freq = freq)+'\n')
            f.writelines('GeraNetwork=1,GeranFreqGroup={freq}'.format(freq = freq)+'\n')
            f.writelines('2' + '\n')
            f.writelines('\n')


def add_csfall_back2utran(file,freq,cellnames):
    with open(file,'a') as f:
        for cellname in cellnames:
            f.writelines('crn ENodeBFunction=1,EUtranCellFDD={cellname},UtranFreqRelation={freq}'.format(cellname = cellname,freq = freq)+'\n')
            f.writelines('cellReselectionPriority 0'+'\n')
            f.writelines('connectedModeMobilityPrio -1'+'\n')
            f.writelines('csFallbackPrio 2'+'\n')
            f.writelines('csFallbackPrioEC 0'+'\n')
            f.writelines('mobilityAction 2'+'\n')
            f.writelines('mobilityActionCsfb 1'+'\n')
            f.writelines('utranFrequencyRef ENodeBFunction=1,UtraNetwork=1,UtranFrequency={}'.format(freq)+'\n')
            f.writelines('end' + '\n')
            f.writelines('\n')


enbs = list(set(df_cs_fallback['ENODEBID'].unique()))
for enb in enbs:
    df_tmp1 = df_cs_fallback[df_cs_fallback['ENODEBID']==enb]
    if df_tmp1['回落策略'].unique()[0] == '2G':
        band = int(df_tmp1['频段'].unique()[0])
        freq_list = df_tmp1['频点'].unique()[0].split(',')
        freq_list = [x for x in freq_list if x!= '']
        add_gran_freq_group(
                './{}.mos'.format(enb),
                band
                )
        for freq in freq_list:
            add_gran_freq(
                    './{}.mos'.format(enb),
                    band,
                    freq
                    )
        add_csfall_back2gran(
                './{}.mos'.format(enb),
                band,
                df_tmp1['cellName']
                )
    else:
        freq = df_tmp1['频点'].unique()[0]
        add_utran_freq(
                './{}.mos'.format(enb),
                freq
                )
        add_csfall_back2utran(
            './{}.mos'.format(enb),
            freq,
            df_tmp1['cellName']
            )
